<?php

/*
 * Edit by shengxin
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace api\admin\controller;

use cmf\controller\RestAdminBaseController;
use api\mall\model\TaskConfigModel;
use api\mall\model\MedalModel;

/**
 * Description of TaskController
 *
 * @author PC
 */
class TaskController extends RestAdminBaseController{
    public function save(){
        $model = new TaskConfigModel();
        $data = $this->request->post();
        $id = $data['id'];
        unset($data['id']);
        $rule = [
            'task_key'=>'require',
            'task_req'=>'require|integer|gt:0',
            'task_reward'=>'require|integer|gt:0',
            'task_name'=>'require',
            'task_desc'=>'require'
        ];
        $message = [
            'task_key'=>'任务类型必须填写',
//            'task_req'=>'任务目标,必须大于0',
            'task_reward'=>'任务奖励,大于0',
            'task_name'=>'任务名称必须填写',
            'task_desc'=>'任务内容必须填写'];
        $rs = $this->validate($data, $rule,$message);
        if($rs!==true){
            $this->error($rs);
        }
        if($data['task_key']=='PromoteTask'){
            $data['task_type']=2;
        }
        if($id==0){
            $model->insert($data);
        }else{
            $model->where('id',$id)->update($data);
        }
        $this->success('保存成功');
    }
    
    public function del(){
        $id = $this->request->route('id/d',0);
        $model = new TaskConfigModel();
        $model->where('id',$id)->delete();
        $this->success('删除成功',$id);
    }
    
    public function save_sign_in(){
        $data = $this->request->post('rewards');
        cmf_set_option('mall_sign_in', $data,true);
        $this->success('ok',$data);
    }
    
    public function valve(){
        $valve = $this->request->get('valve/d',0);
        if($valve>0){
            cmf_set_option('mall_task_valve', ['max'=>$valve]);
        }
        $this->success('ok',$valve);
    }
    
    public function save_medal(){
        $model = new MedalModel();
        $data = $this->request->post();
        $id = $data['id'];
        unset($data['id']);
        $rule = [
            'medal_key'=>'require',
            'medal_name'=>'require',
            'medal_url'=>'require',
            'medal_desc'=>'require'
        ];
        $message = [
            'medal_key'=>'勋章编号必须填写',
//            'task_req'=>'任务目标,必须大于0',
            'medal_name'=>'勋章名称必须填写',
            'medal_url'=>'勋章图标',
            'medal_desc'=>'勋章描述哪去了'];
        $rs = $this->validate($data, $rule,$message);
        if($rs!==true){
            $this->error($rs);
        }
        if($id==0){
            $data['create_time'] = time();
            $model->insert($data);
        }else{
            $model->where('id',$id)->update($data);
        }
        $this->success('保存成功');
    }
    
    public function del_medal(){
        $model = new MedalModel();
        $id = $this->request->route('id/d',0);
        $model->where('id',$id)->delete();
        $this->success('删除成功');
    }
}
